﻿#include "loginwidget.h"
#include "ui_loginwidget.h"
#include <QMessageBox>
#include <QDebug>
#include "tools.h"
#include "user.h"
#include "company.h"
#include "product.h"
#include "userrelation.h"
#include "dbhelper.h"

LoginWidget::LoginWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LoginWidget)
{
    ui->setupUi(this);

    setWindowTitle("登录");

    bool isSave = User::getIsSave();
    if(isSave){
        QString username = User::getUsername();
        QString password = User::getPassword();
        ui->lineEditUsername->setText(username);
        ui->lineEditPassword->setText(password);
        ui->checkBoxRemPassword->setCheckState(Qt::Checked);
    }

    connect(ui->labelRegister, &ClickedLabel::clicked,
            [=]()
    {
        registerWidget.showAndInit();
    });

    connect(ui->labelUserTrade, &ClickedLabel::clicked,
            [=]()
    {
        userTradeWidget.exec();
    });

    connect(ui->labelFindPassword, &ClickedLabel::clicked,
            [=]()
    {
        findPasswordWidget.showAndInit();
    });

    connect(&mainWindow, &MainWindow::showLogin,
            [=]()
    {
       this->show();
    });
}

LoginWidget::~LoginWidget()
{
    delete ui;
}

void LoginWidget::on_pushButtonCancel_clicked()
{
    this->close();
}

void LoginWidget::on_pushButtonLogin_clicked()
{
    ui->pushButtonLogin->setEnabled(false);
    QString username = ui->lineEditUsername->text();
    QString password = ui->lineEditPassword->text();

    manager = new UserNetworkAccessManager();

    connect(manager, &BaseNetworkAccessManager::onSuccess, this, &LoginWidget::on_loginSuccess);

    connect(manager, &BaseNetworkAccessManager::onFailed,
            [=](const QString message)
    {
        ui->pushButtonLogin->setEnabled(true);
        QMessageBox::warning(this, "警告", message);
    });

    manager->login(username, password);
}

void LoginWidget::on_loginSuccess(const QString loginMessage)
{
    ui->pushButtonLogin->setEnabled(true);
    manager = new UserNetworkAccessManager();

    connect(manager, &BaseNetworkAccessManager::onSuccessWithData,
            [=](QString message, QJsonObject data)
    {
        Q_UNUSED(message);
        QJsonObject userObject = data.value("user").toObject();
        User user;
        user.loadFromJsonObject(userObject);
        user.saveToSettings();
        if(data.contains("company"))
        {
            QJsonObject companyObject = data.value("company").toObject();
            Company company;
            company.loadFromJsonObject(companyObject);
            company.saveToSettings();
        }
        DbHelper helper;
        helper.clearProduct();
        if(data.contains("products")){
            QJsonArray productArr = data.value("products").toArray();
            for(QJsonValue obj:productArr){
                Product product;
                product.loadFromJsonObject(obj.toObject());
                helper.insertProduct(product);
            }
        }

        helper.clearUserRelation();
        if(data.contains("relations")){
            QJsonArray relationArr = data.value("relations").toArray();
            for(QJsonValue obj:relationArr){
                UserRelation relation;
                relation.loadFromJsonObject(obj.toObject());
                helper.insertUserRelation(relation);
            }
        }

        helper.clearFormula();
        if(data.contains("formulas")){
            QJsonArray formulaArr = data.value("formulas").toArray();
            for(QJsonValue obj:formulaArr){
                FormulaBase formulaBase;
                formulaBase.loadFromJsonObject(obj.toObject());
                helper.insertFormula(formulaBase);
            }
        }

        this->close();
        mainWindow.initAfterLoginOK();
        mainWindow.show();
        QMessageBox::information(this, "消息", loginMessage);
    });

    if(ui->checkBoxRemPassword->isChecked())
    {
        //保存密码
        User::setUsername(ui->lineEditUsername->text());
        User::setPassword(ui->lineEditPassword->text());
        User::setIsSave(true);
    }else{
        User::setIsSave(false);
    }

    manager->updateUserInfo();
}
